﻿using System;
using System.Data.SqlClient;
using System.Data;
using System.Data.Common;
using System.Configuration;
//using Microsoft.Practices.EnterpriseLibrary.Data;

namespace DAL
{
    public class DataService
    {
        /*static string strConStr = ConfigurationManager.ConnectionStrings["SQLConnectionString"].ConnectionString;

        public static SqlConnection connection;
        public DataService()
        {
            if (connection == null) { connection = new SqlConnection(strConStr); }
        }
        public static SqlConnection GetConnection()
        {
            if (connection.State == ConnectionState.Closed)
            {
                connection.Open();
                return connection;
            }
            else
                return connection;
        }
        Database db = DatabaseFactory.CreateDatabase();

        public void ExecuteNonQuery(string sql)
        {
            DbCommand cmd = db.GetSqlStringCommand(sql);
            try
            {
                db.ExecuteNonQuery(cmd);
            }
            catch (Exception ex)
            {
                //throw ex;
            }
            finally
            {
                if (cmd != null) cmd.Dispose();
            }
        }

        public string MaxId(string Table, string ColId)
        {
            object strReturn = "";
            DbCommand cmd = db.GetSqlStringCommand("SELECT max(" + ColId + ") as maxid FROM " + Table);
            try
            {
                strReturn = db.ExecuteScalar(cmd);
            }
            catch (Exception ex)
            {
                //throw ex;
            }
            finally
            {
                if (cmd != null) cmd.Dispose();
            }
            return strReturn.ToString();
        }*/


        public static string strcon = ConfigurationManager.ConnectionStrings["strcon"].ConnectionString.ToString();
        public static SqlConnection con = new SqlConnection(strcon);
        public DataService()
        {
            if (con == null)
            {
                con = new SqlConnection(strcon);
            }
        }

        public SqlConnection getConn()
        {
            if (con.State == ConnectionState.Closed)
            {
                con.Open();
                return con;
            }
            else
                return con;

        }
       /* public static int executeQuery(string query, CommandType type, SqlParameter[] pa)
        {
            SqlCommand com = new SqlCommand(query, getConn());
            com.CommandType = type;
            foreach (SqlParameter pas in pa)
            {
                com.Parameters.Add(pas);
            }
            return com.ExecuteNonQuery();


        }
        public static DataSet getDataTable(string sql, CommandType type, SqlParameter[] pa)
        {
            DataSet ds = new DataSet();
            SqlCommand com = new SqlCommand(sql, getConn());
            com.CommandType = type;
            SqlDataAdapter da = new SqlDataAdapter();


            if (pa == null)
            {
                da.SelectCommand = com;

                da.Fill(ds);
                return ds;

            }
            else
            {

                foreach (SqlParameter pas in pa)
                {
                    com.Parameters.Add(pas);
                }
                da.SelectCommand = com;


                da.Fill(ds);
                return ds;


            }
        }*/
    }
}
